package com.luckyframe.project.jmeter.jmeterAlertConfig.controller;

import com.luckyframe.common.utils.poi.ExcelUtil;
import com.luckyframe.framework.aspectj.lang.annotation.Log;
import com.luckyframe.framework.aspectj.lang.enums.BusinessType;
import com.luckyframe.framework.web.controller.BaseController;
import com.luckyframe.framework.web.domain.AjaxResult;
import com.luckyframe.framework.web.page.TableDataInfo;
import com.luckyframe.project.jmeter.jmeterAlertConfig.domain.JmeterAlertConfig;
import com.luckyframe.project.jmeter.jmeterAlertConfig.service.IJmeterAlertConfigService;
import com.luckyframe.project.jmeter.jmeterTestCase.domain.JmeterTestCase;
import com.luckyframe.project.jmeter.jmeterTestCase.service.IJmeterTestCaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 告警配置 信息操作处理
 * 
 * @author lancelot
 * @date 2020-11-23
 */
@Controller
@RequestMapping("/jmeter/jmeterAlertConfig")
public class JmeterAlertConfigController extends BaseController
{
    private String prefix = "jmeter/jmeterAlertConfig";
	
	@Autowired
	private IJmeterAlertConfigService jmeterAlertConfigService;

	@Autowired
	private IJmeterTestCaseService jmeterTestCaseService;



	@RequiresPermissions("jmeter:jmeterAlertConfig:view")
	@GetMapping()
	public String jmeterAlertConfig(@RequestParam(value = "caseId",required = false)Long caseId, ModelMap modelMap)
	{

		List<JmeterTestCase> cases = jmeterTestCaseService.selectJmeterTestCaseList(new JmeterTestCase());
		modelMap.put("caseId",caseId);
		modelMap.put("cases",cases);
		return prefix + "/jmeterAlertConfig";
	}
	
	/**
	 * 查询告警配置列表
	 */
	@RequiresPermissions("jmeter:jmeterAlertConfig:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(JmeterAlertConfig jmeterAlertConfig)
	{
		startPage();
        List<JmeterAlertConfig> list = jmeterAlertConfigService.selectJmeterAlertConfigList(jmeterAlertConfig);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出告警配置列表
	 */
	@RequiresPermissions("jmeter:jmeterAlertConfig:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JmeterAlertConfig jmeterAlertConfig)
    {
    	List<JmeterAlertConfig> list = jmeterAlertConfigService.selectJmeterAlertConfigList(jmeterAlertConfig);
        ExcelUtil<JmeterAlertConfig> util = new ExcelUtil<JmeterAlertConfig>(JmeterAlertConfig.class);
        return util.exportExcel(list, "jmeterAlertConfig");
    }
	
	/**
	 * 新增告警配置
	 */
	@GetMapping("/add")
	public String add(ModelMap mmp)
	{

		List<JmeterTestCase> cases = jmeterTestCaseService.selectJmeterTestCaseList(new JmeterTestCase());
		mmp.put("cases",cases);
		return prefix + "/add";
	}
	
	/**
	 * 新增保存告警配置
	 */
	@RequiresPermissions("jmeter:jmeterAlertConfig:add")
	@Log(title = "告警配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(JmeterAlertConfig jmeterAlertConfig)
	{

		Long caseId = jmeterAlertConfig.getCaseId();
		JmeterTestCase testCase = jmeterTestCaseService.selectJmeterTestCaseById(caseId);

		if(testCase!=null){
			jmeterAlertConfig.setCaseName(testCase.getCaseName());
		}

		return toAjax(jmeterAlertConfigService.insertJmeterAlertConfig(jmeterAlertConfig));
	}

	/**
	 * 修改告警配置
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		JmeterAlertConfig jmeterAlertConfig = jmeterAlertConfigService.selectJmeterAlertConfigById(id);

		List<JmeterTestCase> cases = jmeterTestCaseService.selectJmeterTestCaseList(new JmeterTestCase());

		mmap.put("cases",cases);
		mmap.put("jmeterAlertConfig", jmeterAlertConfig);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存告警配置
	 */
	@RequiresPermissions("jmeter:jmeterAlertConfig:edit")
	@Log(title = "告警配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(JmeterAlertConfig jmeterAlertConfig)
	{

		Long caseId = jmeterAlertConfig.getCaseId();
		JmeterTestCase testCase = jmeterTestCaseService.selectJmeterTestCaseById(caseId);

		if(testCase!=null){
			jmeterAlertConfig.setCaseName(testCase.getCaseName());
		}

		return toAjax(jmeterAlertConfigService.updateJmeterAlertConfig(jmeterAlertConfig));
	}
	
	/**
	 * 删除告警配置
	 */
	@RequiresPermissions("jmeter:jmeterAlertConfig:remove")
	@Log(title = "告警配置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(jmeterAlertConfigService.deleteJmeterAlertConfigByIds(ids));
	}
	
}
